package com.lib.krismile.base.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/*
 * @author ： wangJiaWei
 * @date : 2021/3/31  15:59
 * @description ：RecycleView公用适配器 结合dataBinding
 */
public class BaseCommonAdapter<T> extends BaseQuickAdapter<T, BaseCommonViewHolder> {

    /**
     * 实体类中绑定的 variable 中的 name
     */
    private final int variableId;

    /**
     * layout 资源文件
     */
    private int layoutResId;

    public BaseCommonAdapter(int layoutResId, int variableId) {
        super(layoutResId);
        this.layoutResId = layoutResId;
        this.variableId = variableId;
    }

    public BaseCommonAdapter(int layoutResId, List<T> list, int variableId) {
        super(layoutResId, list);
        this.variableId = variableId;
    }


    @Override
    protected void onItemViewHolderCreated(@NotNull BaseCommonViewHolder viewHolder, int viewType) {
        super.onItemViewHolderCreated(viewHolder, viewType);
        DataBindingUtil.bind(viewHolder.itemView);

    }

    @Override
    protected void convert(@NotNull BaseCommonViewHolder baseHolder, T entity) {
        //这里的逻辑是只是处理已经加载到屏幕中的item 数据。
        switch (baseHolder.getItemViewType()) {
            case LOAD_MORE_VIEW:
                // 加载更多的view
                break;
            case HEADER_VIEW:
                // 头部控件
                break;
            case EMPTY_VIEW:
                // 空布局
                break;
            case FOOTER_VIEW:
                // 尾部
                break;
            default:
                baseHolder.binding.setVariable(variableId, entity);
                //防止数据闪烁
                baseHolder.binding.executePendingBindings();
                break;
        }
    }
}
